/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClienteSelector.java
 *
 * Created on Apr 9, 2009, 8:46:27 AM
 */

package doghost.selectors;

import javax.swing.RowFilter;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;
import doghost.controllers.ClienteController;
import doghost.entities.Cliente;

/**
 *
 * @author duo
 */
public class ClienteSelector extends javax.swing.JDialog {

    protected ClienteController clienteController;
    protected Cliente selectedCliente;

    /** Creates new form ClienteSelector */
    public ClienteSelector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        clienteController = new ClienteController();
        clienteTable.getSelectionModel().addListSelectionListener(
                new ListSelectionListener() {


            @Override
                    public void valueChanged(ListSelectionEvent e) {
                        firePropertyChange("clienteRecordSelected",
                                !isClienteRecordSelected(), isClienteRecordSelected());
                    }
                });
    }

    public Cliente getSelectedCliente() {
        return selectedCliente;
    }

    public void setSelectedCliente(Cliente selectedCliente) {
        this.selectedCliente = selectedCliente;
    }

    public boolean isClienteRecordSelected() {
        return clienteTable.getSelectedRow() != -1;
    }

    @Action
    public void newCliente() {
        clienteController.newCliente(doghostPUEntityManager, clienteList);
    }

    @Action(enabledProperty = "clienteRecordSelected")
    public void editCliente() {
        int index = clienteTable.getSelectedRow();
        clienteController.setCliente(clienteList.get(clienteTable.
                convertRowIndexToModel(index)));
        clienteController.editCliente(doghostPUEntityManager, clienteList,
                clienteTable.convertRowIndexToModel(index));
    }

    @Action(enabledProperty = "clienteRecordSelected")
    public void deleteCliente() {
        int index = clienteTable.getSelectedRow();
        clienteController.setCliente(clienteList.get(clienteTable.
            convertRowIndexToModel(index)));
        clienteController.deleteCliente(doghostPUEntityManager, clienteList,
            clienteTable.convertRowIndexToModel(index));
    }

    @Action
    public void cancelClienteSelector() {
        setSelectedCliente(null);
        setVisible(false);
    }

    @Action(enabledProperty = "clienteRecordSelected")
    public void selectCliente() {
        int index = clienteTable.getSelectedRow();
        setSelectedCliente(clienteList.get(clienteTable.
                convertRowIndexToModel(index)));
        setVisible(false);
    }

    @Action
    public void filterCliente() {
        TableRowSorter clienteSorter = (TableRowSorter) clienteTable.getRowSorter();
        String text = clienteFilterTextField.getText();
        clienteFilterTextField.setText("");
        if(text.length()==0) {
            clienteSorter.setRowFilter(null);
        } else {
            clienteSorter.setRowFilter(RowFilter.regexFilter("(?u)(?i)" + text));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        doghostPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("doghostPU").createEntityManager();
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getResourceMap(ClienteSelector.class);
        clienteQuery = java.beans.Beans.isDesignTime() ? null : doghostPUEntityManager.createQuery(resourceMap.getString("clienteQuery.query")); // NOI18N
        clienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(clienteQuery.getResultList());
        clienteFilterTextField = new javax.swing.JTextField();
        clienteFilterButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        clienteSelectionPanel = new org.jdesktop.swingx.JXPanel();
        clienteScrollPane = new javax.swing.JScrollPane();
        clienteTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        clienteFilterTextField.setName("clienteFilterTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(doghost.DoghostApp.class).getContext().getActionMap(ClienteSelector.class, this);
        clienteFilterButton.setAction(actionMap.get("filterCliente")); // NOI18N
        clienteFilterButton.setName("clienteFilterButton"); // NOI18N

        newButton.setAction(actionMap.get("newCliente")); // NOI18N
        newButton.setName("newButton"); // NOI18N

        editButton.setAction(actionMap.get("editCliente")); // NOI18N
        editButton.setName("editButton"); // NOI18N

        deleteButton.setAction(actionMap.get("deleteCliente")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N

        cancelButton.setAction(actionMap.get("cancelClienteSelector")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        clienteSelectionPanel.setName("clienteSelectionPanel"); // NOI18N

        clienteScrollPane.setName("clienteScrollPane"); // NOI18N

        clienteTable.setAutoCreateRowSorter(true);
        clienteTable.setName("clienteTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, clienteList, clienteTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nome}"));
        columnBinding.setColumnName("Nome");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fone}"));
        columnBinding.setColumnName("Fone");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mobile}"));
        columnBinding.setColumnName("Mobile");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${email}"));
        columnBinding.setColumnName("Email");
        columnBinding.setColumnClass(String.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        clienteScrollPane.setViewportView(clienteTable);
        clienteTable.getColumnModel().getColumn(0).setMinWidth(150);
        clienteTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        clienteTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("clienteTable.columnModel.title0")); // NOI18N
        clienteTable.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("clienteTable.columnModel.title1")); // NOI18N
        clienteTable.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("clienteTable.columnModel.title2")); // NOI18N
        clienteTable.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("clienteTable.columnModel.title3")); // NOI18N

        javax.swing.GroupLayout clienteSelectionPanelLayout = new javax.swing.GroupLayout(clienteSelectionPanel);
        clienteSelectionPanel.setLayout(clienteSelectionPanelLayout);
        clienteSelectionPanelLayout.setHorizontalGroup(
            clienteSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 630, Short.MAX_VALUE)
            .addGroup(clienteSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(clienteSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(clienteScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 606, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        clienteSelectionPanelLayout.setVerticalGroup(
            clienteSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
            .addGroup(clienteSelectionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(clienteSelectionPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(clienteScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jButton1.setAction(actionMap.get("selectCliente")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(clienteFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(clienteFilterButton))
                            .addComponent(clienteSelectionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 333, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(23, 23, 23))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clienteFilterTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(clienteFilterButton))
                .addGap(18, 18, 18)
                .addComponent(clienteSelectionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(editButton)
                    .addComponent(deleteButton)
                    .addComponent(cancelButton)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ClienteSelector dialog = new ClienteSelector(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton clienteFilterButton;
    private javax.swing.JTextField clienteFilterTextField;
    private java.util.List<doghost.entities.Cliente> clienteList;
    private javax.persistence.Query clienteQuery;
    private javax.swing.JScrollPane clienteScrollPane;
    private org.jdesktop.swingx.JXPanel clienteSelectionPanel;
    private javax.swing.JTable clienteTable;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager doghostPUEntityManager;
    private javax.swing.JButton editButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton newButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
